package com.lzj.jiangxindaojia.view.login;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzj.jiangxindaojia.R;
import com.lzj.jiangxindaojia.base.BaseActivity;
import com.lzj.jiangxindaojia.bean.Common;
import com.lzj.jiangxindaojia.bean.ResultCode;
import com.lzj.jiangxindaojia.http.HttpAction;
import com.lzj.jiangxindaojia.http.HttpCallBack;
import com.lzj.jiangxindaojia.http.HttpConstans;
import com.lzj.jiangxindaojia.http.HttpUtil;
import com.lzj.jiangxindaojia.utils.GsonUtil;
import com.lzj.jiangxindaojia.utils.MD5;
import com.lzj.jiangxindaojia.utils.RegexUtils;
import com.lzj.jiangxindaojia.utils.Utils;

import java.util.HashMap;

/**
 * Created by lizj on 2017/12/5.
 */

public class ResetpwdActivity extends BaseActivity implements HttpCallBack{
    private TextView tv_tile,tv_getyanzhnengma, pwd_hide, okpwd_hide;
    private ImageView iv_left;
    private LinearLayout ll_left;
    private Button btn_ok;
    private EditText reset_phone,reset_yanzhengma,reset_pwd,pwd_ok;
    @Override
    protected void initData() {
        super.initData();
        this.tv_tile = ((TextView)findViewById(R.id.tv_tile));
        this.iv_left = ((ImageView)findViewById(R.id.iv_left));
        this.ll_left = ((LinearLayout)findViewById(R.id.ll_left));
        this.tv_tile.setText(R.string.resetpwd_title);
        iv_left.setImageResource(R.mipmap.back);
        this.ll_left.setVisibility(View.VISIBLE);

        this.tv_getyanzhnengma = ((TextView)findViewById(R.id.tv_getyanzhnengma));
        this.reset_phone = ((EditText)findViewById(R.id.reset_phone));
        this.reset_yanzhengma = ((EditText)findViewById(R.id.reset_yanzhengma));
        this.reset_pwd = ((EditText)findViewById(R.id.reset_pwd));
        this.pwd_ok = ((EditText)findViewById(R.id.pwd_ok));
        this.btn_ok = ((Button)findViewById(R.id.btn_ok));

        this.pwd_hide = ((TextView) findViewById(R.id.pwd_hide));
        this.okpwd_hide = ((TextView) findViewById(R.id.okpwd_hide));


    }

    @Override
    protected void requestServer() {
        super.requestServer();
    }

    @Override
    protected void setListener() {
        super.setListener();
        btn_ok.setOnClickListener(this);
        ll_left.setOnClickListener(this);
        tv_getyanzhnengma.setOnClickListener(this);
        pwd_hide.setOnClickListener(this);
        okpwd_hide.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_resetpwd;
    }


    private String tel,pwd,yzm,pwdok;
    HashMap paramMap;
    private boolean pwdflag = true;
    private boolean okpwdflag = true;
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.pwd_hide:
                if (pwdflag) {
                    //显示密码
                    reset_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdflag = false;
                    Utils.setDrawableLeft(this,R.mipmap.regist_open,pwd_hide);
                } else {
                    //否则隐藏密码
                    reset_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdflag = true;
                    Utils.setDrawableLeft(this,R.mipmap.regist_close,pwd_hide);
                }
                break;
            case R.id.okpwd_hide:
                if (okpwdflag) {
                    //显示密码
                    pwd_ok.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    okpwdflag = false;
                    Utils.setDrawableLeft(this,R.mipmap.regist_open,okpwd_hide);
                } else {
                    //否则隐藏密码
                    pwd_ok.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    okpwdflag = true;
                    Utils.setDrawableLeft(this,R.mipmap.regist_close,okpwd_hide);
                }
                break;
            case R.id.btn_ok:
                this.tel = this.reset_phone.getText().toString().trim();
                this.yzm = this.reset_yanzhengma.getText().toString().trim();
                this.pwd = this.reset_pwd.getText().toString().trim();
                this.pwdok = this.pwd_ok.getText().toString().trim();
                if (TextUtils.isEmpty(this.tel)) {
                    Utils.showToast(this, getResources().getString(R.string.please_input_uname));
                    return;
                }
                if (TextUtils.isEmpty(this.yzm)) {
                    Utils.showToast(this, getResources().getString(R.string.please_input_yanzhengma));
                    return;
                }
                if (TextUtils.isEmpty(this.pwd)) {
                    Utils.showToast(this, getResources().getString(R.string.please_input_pwd));
                    return;
                }
                if(!RegexUtils.ispwd(pwd)){
                    Utils.showToast(this, getResources().getString(R.string.please_input_regpwd));
                    return;
                }

                if (TextUtils.isEmpty(this.pwdok)) {
                    Utils.showToast(this, getResources().getString(R.string.please_ok_passwd));
                    return;
                }

                if (!pwd.equals(pwdok))
                {
                    Utils.showToast(this, getResources().getString(R.string.pwd_different));
                    return;
                }
                paramMap = new HashMap();
                paramMap.put("channel_number", HttpConstans.qdnumber);
                paramMap.put("phone", tel);
                paramMap.put("verify_code", yzm);
                paramMap.put("password", pwd);
                paramMap.put("sign", MD5.md5(MD5.createSignString(paramMap) + HttpConstans.KEY));
                HttpUtil.getInstance(this).postAsynHttp(HttpConstans.BASEURL+HttpConstans.RESET_PASSWORD,paramMap,this,this, HttpAction.RESET_PASSWORD);
                break;
            case R.id.ll_left:
                finish();
                break;
            case R.id.tv_getyanzhnengma:
                this.tel = this.reset_phone.getText().toString();
                if (TextUtils.isEmpty(this.tel)) {
                    Utils.showToast(this, getResources().getString(R.string.please_input_phone));
                    return;
                }
                if (!RegexUtils.isMobileNO(this.tel)) {
                    Utils.showToast(this, getResources().getString(R.string.please_input_okphone));
                    return;
                }
                paramMap = new HashMap();
                paramMap.put("app_type", "2");
                paramMap.put("system_type", "2");
                paramMap.put("phone", tel);
                paramMap.put("action_type", "forgotpassword");
                paramMap.put("qd_app_id", HttpConstans.qdnumber);
                paramMap.put("sign", MD5.md5(MD5.createSignString(paramMap) + HttpConstans.KEY));
                HttpUtil.getInstance(this).postAsynHttp(HttpConstans.GETYANZHNEGMA, paramMap, this, this, HttpAction.GETYANZHENGMA);
                break;
        }
    }


    Common common;
    @Override
    public <T> void onSuccess(String  result, String action) {
        switch (action) {
            case HttpAction.GETYANZHENGMA:
                common = GsonUtil.GsonToBean(result, Common.class);
                if (ResultCode.SUCCESS.equals(common.getStatus())) {
                    new TimeCount(60000L, 1000L).start();
                } else {
                    Utils.showToast(this, common.getMsg());
                }
                break;
            case HttpAction.RESET_PASSWORD:
                common = GsonUtil.GsonToBean(result, Common.class);
                if (ResultCode.SUCCESS.equals(common.getStatus())) {
                    Utils.showToast(this, common.getMsg());
                    finish();
                } else {
                    Utils.showToast(this, common.getMsg());
                }
                break;
        }
    }

    // timer Util
    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            tv_getyanzhnengma.setText("重新验证");
            tv_getyanzhnengma.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            tv_getyanzhnengma.setClickable(false);
            tv_getyanzhnengma.setText(millisUntilFinished / 1000 + "秒");
        }
    }
}
